<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="任务类型" prop="developmentType">
    <el-select v-model="queryParams.developmentType" placeholder="请选择任务类型" clearable>
      <el-option
          v-for="dict in developmentTypeOptions"
          :key="dict.enumCode"
          :label="dict.description"
          :value="dict.enumCode"
      />
    </el-select>
  </el-form-item> -->
      <el-form-item label="业务模块" prop="module">
        <el-input
          v-model="queryParams.module"
          placeholder="请输入业务模块"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指派给" prop="mainHandlerId">
        <LoadPersonSelect
          :placeholder="'请选择申请人'"
          @getSelectedParty="getSelectedParty"
        ></LoadPersonSelect>
      </el-form-item>
      <el-form-item label="任务状态" prop="statusId">
        <el-select
          v-model="queryParams.statusId"
          placeholder="请选择任务状态"
          clearable
        >
          <el-option
            v-for="dict in statusIdOptions"
            :key="dict.enumCode"
            :label="dict.description"
            :value="dict.enumCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dev_task:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dev_task:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dev_task:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dev_task:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dev_taskList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="任务ID"
        align="left"
        prop="developmentId"
        width="70"
      />
      <el-table-column
        label="任务类型"
        align="left"
        prop="developmentType"
        width="80"
        :formatter="developmentTypeFormat"
      />
      <el-table-column label="模块/功能" align="left" prop="module" />
      <el-table-column
        label="任务描述"
        align="left"
        prop="developmentDes"
        width="300"
      />
      <!-- <el-table-column label="创建人" align="left" prop="creatorId" width="80" :formatter="creatorIdFormat"/> -->
      <el-table-column
        label="指派给"
        align="left"
        prop="mainHandlerId"
        width="120"
        :formatter="mainHandlerIdFormat"
      />
      <el-table-column
        label="任务状态"
        align="left"
        prop="statusId"
        width="80"
        :formatter="statusIdFormat"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="lastUpdatedStamp"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.lastUpdatedStamp, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createdStamp" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.createdStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:dev_task:detail']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dev_task:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dev_task:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改开发任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        v-bind:disabled="detailFlag"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="developmentType">
              <el-select
                v-model="form.developmentType"
                placeholder="请选择任务类型"
              >
                <el-option
                  v-for="dict in developmentTypeOptions"
                  :key="dict.enumCode"
                  :label="dict.description"
                  :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模块/功能" prop="module">
              <el-input v-model="form.module" placeholder="请输入模块/功能" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务描述">
              <editor v-model="form.developmentDes" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="创建人" prop="creatorId">
              <el-select
                v-model="form.creatorId"
                placeholder="请选择创建人"
                disabled
              >
                <el-option
                  v-for="dict in creatorIdOptions"
                  :key="dict.partyId"
                  :label="dict.nickName"
                  :value="dict.partyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指派给" prop="mainHandlerId">
              <el-select
                v-model="form.mainHandlerId"
                placeholder="请选择指派给"
              >
                <el-option
                  v-for="dict in mainHandlerIdOptions"
                  :key="dict.partyId"
                  :label="dict.nickName"
                  :value="dict.partyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="任务状态" prop="statusId">
              <el-radio-group v-model="form.statusId">
                <el-radio
                  v-for="dict in statusIdOptions"
                  :key="dict.enumCode"
                  :label="dict.enumCode"
                  >{{ dict.description }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="附件">
              <image-upload v-model="form.affixFilePath" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDev_task,
  getDev_task,
  delDev_task,
  addDev_task,
  updateDev_task,
} from "@/api/system/dev_task";
import LoadPersonSelect from "@/components/LoadPersonSelect";
export default {
  name: "Dev_task",
  components: { LoadPersonSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开发任务表格数据
      dev_taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 任务类型业务字典
      developmentTypeOptions: [],
      // 创建人业务字典
      creatorIdOptions: [],
      // 指派给业务字典
      mainHandlerIdOptions: [],
      // 任务状态业务字典
      statusIdOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        developmentType: null,
        module: null,
        mainHandlerId: null,
        statusId: "dealing",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        developmentType: [
          { required: true, message: "任务类型不能为空", trigger: "change" },
        ],
        developmentDes: [
          { required: true, message: "任务描述不能为空", trigger: "blur" },
        ],
        statusId: [
          { required: true, message: "任务状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 任务类型业务字典
    this.getEnumDicts("SYS_DEV_TASK_TYPE").then((response) => {
      this.developmentTypeOptions = response.data;
    });
    // 创建人业务字典
    this.getEntityDicts("SysUser:partyId:nickName").then((response) => {
      this.creatorIdOptions = response.data;
      this.mainHandlerIdOptions = response.data;
    });
    // 任务状态业务字典
    this.getEnumDicts("SYS_DEV_TASK_STATUS").then((response) => {
      this.statusIdOptions = response.data;
      // 查询条件中的 任务状态默认选中
    });
    this.getList();
  },
  methods: {
    getSelectedParty(e) {
      // console.log("打印e组件选择值",e)
      this.queryParams.partyId = e;
    },
    developmentTypeFormat(row, column) {
      let val = null;
      this.developmentTypeOptions.forEach((item, index) => {
        if (row.developmentType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    creatorIdFormat(row, column) {
      let val = null;
      this.creatorIdOptions.forEach((item, index) => {
        if (row.creatorId === item.partyId) {
          val = item.nickName;
        }
      });
      return val;
    },
    mainHandlerIdFormat(row, column) {
      let val = null;
      this.mainHandlerIdOptions.forEach((item, index) => {
        if (row.mainHandlerId === item.partyId) {
          val = item.nickName;
        }
      });
      return val;
    },
    statusIdFormat(row, column) {
      let val = null;
      this.statusIdOptions.forEach((item, index) => {
        if (row.statusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询开发任务列表 */
    getList() {
      this.loading = true;
      // console.info(this.queryParams);
      listDev_task(this.queryParams).then((response) => {
        this.dev_taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        developmentId: null,
        developmentType: null,
        module: null,
        developmentDes: null,
        creatorId: null,
        creatorName: null,
        mainHandlerId: null,
        mainHandlerName: null,
        auxiliaryHandlerName: null,
        auxiliaryHandlerId: null,
        statusId: "",
        remark: null,
        affixFilePath: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.developmentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      (this.editFlag = true), (this.updateFlag = false);
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加开发任务";
      // 新增时，给创建人赋值
      this.form.creatorId = this.$store.state.user.partyId;
      this.form.statusId = "10090";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      (this.editFlag = false), (this.updateFlag = false);
      this.detailFlag = true;
      this.reset();
      const developmentId = row.developmentId || this.ids;
      getDev_task(developmentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "开发任务详情";
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const developmentId = row.developmentId || this.ids;
      getDev_task(developmentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开发任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.developmentId != null && this.updateFlag) {
            updateDev_task(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDev_task(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const developmentIds = row.developmentId || this.ids;
      this.$modal
        .confirm('是否确认删除开发任务编号为"' + developmentIds + '"的数据项？')
        .then(function () {
          return delDev_task(developmentIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dev_task/export",
        {
          ...this.queryParams,
        },
        `dev_task_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
